<?php

declare (strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Model;

use Hyperf\Database\Model\SoftDeletes;
use Hyperf\Database\Query\Builder;
use Qbhy\HyperfAuth\Authenticatable;
class User extends Model implements Authenticatable
{
//    use Authenticatable;
    use SoftDeletes;
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected ?string $table = 'user';
    public static $statusList = [1 => '启用', -1 => '禁用'];
    //增加 jwt接口需要的方法
//    public function getJwtCustomClaims() : array
//    {
//        return ['guard' => 'api'];
//    }
//    public function getJwtIdentifier()
//    {
//        return $this->getKey();
//    }

    public function getId()
    {
        return $this->id;
    }

    public static function retrieveById($key): ?Authenticatable
    {
        return self::query()->find($key);
    }
    /**
     * 头像
     * @return \Hyperf\Database\Model\Relations\BelongsTo
     */
    public function header()
    {
        return $this->belongsTo(Upload::class, 'header_img_id');
    }
    /**
     * 通知
     * @return \Hyperf\Database\Model\Relations\HasMany
     */
    public function noticeUser()
    {
        return $this->hasMany(NoticeUser::class, 'user_id');
    }
    /**
     * 订单
     * @return \Hyperf\Database\Model\Relations\HasMany
     */
    public function order()
    {
        return $this->hasMany(Order::class, 'user_id');
    }
    /**
     * 收藏
     * @return \Hyperf\Database\Model\Relations\HasMany
     */
    public function collect()
    {
        return $this->hasMany(Collect::class, 'user_id');
    }
    /**
     * 企业
     * @return \Hyperf\Database\Model\Relations\HasOne
     */
    public function enterprise()
    {
        return $this->hasOne(Enterprise::class, 'user_id');
    }
    /**
     * 企业认证
     * @return \Hyperf\Database\Model\Relations\HasMany
     */
    public function enterpriseCertification()
    {
        return $this->hasMany(EnterpriseCertification::class, 'user_id');
    }
    /**
     * 搜索历史
     * @return \Hyperf\Database\Model\Relations\HasMany
     */
    public function searchHistory()
    {
        return $this->hasMany(SearchHistory::class, 'user_id');
    }
    /**
     * 信息
     * @return \Hyperf\Database\Model\Relations\HasMany
     */
    public function information()
    {
        return $this->hasMany(Information::class, 'user_id');
    }
    /**
     * 信息点赞记录
     * @return \Hyperf\Database\Model\Relations\HasMany
     */
    public function informationLikes()
    {
        return $this->hasMany(InformationLikes::class, 'user_id');
    }
    public function getStatusTextAttribute()
    {
        return static::$statusList[$this->status] ?? '禁用';
    }
    public function scopeActive($q)
    {
        /** @var Builder $q */
        return $q->where('status', 1);
    }
    /**
     * 通过openid查找
     * @param $openid
     * @return \Hyperf\Database\Model\Builder|\Hyperf\Database\Model\Model|object|null
     */
    public static function getByOpenid($openid)
    {
        return static::query()->where('openid', $openid)->first();
    }
    /**
     * 通过手机号查找
     * @param $mobile
     * @return \Hyperf\Database\Model\Builder|\Hyperf\Database\Model\Model|object|null
     */
    public static function getByMobile($mobile)
    {
        return static::query()->where('mobile', $mobile)->first();
    }
    /**
     * 启用/禁用
     * @return $this
     */
    public function handle()
    {
        if ($this->status == 1) {
            $this->status = -1;
        } else {
            $this->status = 1;
        }
        return $this;
    }
    /**
     * 关键字搜索
     * @param $q
     * @param $keyword
     * @return Builder
     */
    public function scopeKeyword($q, $keyword)
    {
        /** @var Builder $q */
        return $q->where(function ($q) use($keyword) {
            /** @var Builder $q */
            $q->where('nickname', 'like', '%' . $keyword . '%');
            $q->orWhere('mobile', 'like', '%' . $keyword . '%');
        });
    }
    /**
     * 接口返回
     * @return array
     */
    public function toApi()
    {
        return [
            'id' => $this->id,
            'openid' => $this->openid ?? '',
            'nickname' => $this->nickname ?? '',
            'header' => stripslashes(($this->header->url ?? '')),
            'header_img_id' => $this->header_img_id ?? '',
            'mobile' => $this->mobile ?? '',
            'is_enterprise_certification' => $this->is_enterprise_certification ?? -1,
            'enterprise' => $this->enterprise->name ?? ''
        ];
    }
}